﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.Security;

using System.Web.Configuration;
using System.Data;
using System.Data.Linq;

using SPU.DataAccess;
using SPU.Entities;

public partial class Dashboard_Profile : System.Web.UI.Page
{
    protected void Page_Load(object sender, EventArgs e)
    {
        bool isLogin = HttpContext.Current.User.Identity.IsAuthenticated;
        if (isLogin)
        {
            string username = HttpContext.Current.User.Identity.Name;
            MembershipUser user = Membership.GetUser(username);

            Label_Value_Username.Text = username;
            Label_Value_Email.Text = user.Email;
            Label_Value_NameLengkap.Text = GetFullName(username);
            Label_Value_NoInduk.Text = GetNomorInduk(username);
            Label_Value_Org.Text = GetOrganisasi(username);
            Label_Value_Alamat.Text = GetAlamat(username);
        }
    }

    #region Additional Methods
    protected String GetFullName(string username)
    {
        ProfileCommon _ProfileCommon = Profile.GetProfile(username);
        string fullname = _ProfileCommon.NamaLengkap;
        if (String.IsNullOrEmpty(fullname.Trim())) return "-";

        return fullname;
    }

    protected String GetNomorInduk(string username)
    {
        ProfileCommon _ProfileCommon = Profile.GetProfile(username);
        string nomorInduk = _ProfileCommon.NomorInduk;
        if (String.IsNullOrEmpty(nomorInduk.Trim())) return "-";

        return nomorInduk;
    }

    protected String GetAlamat(string username)
    {
        ProfileCommon _ProfileCommon = Profile.GetProfile(username);
        string alamat = _ProfileCommon.Alamat;
        if (String.IsNullOrEmpty(alamat.Trim())) return "-";

        return alamat;
    }

    protected String GetOrganisasi(string username)
    {
        ProfileCommon _ProfileCommon = Profile.GetProfile(username);
        string orgID = _ProfileCommon.OrganisasiID;
        string orgNama = String.Empty;

        try
        {
            SPUDataAccess db = new SPUDataAccess();
            Organisasi org = db.SPUDataContext.Organisasis.Where(p => p.OrganisasiID.Equals(orgID)).Single<Organisasi>();

            while (!org.OrganisasiInduk.Equals("0"))
            {
                orgNama = org.OrganisasiNama + "<br/>" + orgNama;
                org = db.SPUDataContext.Organisasis.Where(p => p.OrganisasiID.Equals(org.OrganisasiInduk)).Single<Organisasi>();
            }
        }
        catch
        {
            orgNama = "-";
        }

        return orgNama;
    }
    #endregion Additional Methods
}